package com.ssm.service.imp;

import com.ssm.pojo.UserInformation;
import com.ssm.service.MessageDelivery;
import com.ssm.utils.UserContext;
import com.ssm.wocket.Chat;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service("MessageDeliveryImp")
public class MessageDeliveryImp implements MessageDelivery {
    @Override
    public Boolean sendMes(Long otherId, String mes) throws IOException {
        Long id = UserContext.getUser().getId();
        Map<String,Chat> mps = (Map<String,Chat>) Chat.scoketMap.get(otherId);
        Map mData = new HashMap<>();
        mData.put("type","chat");
        mData.put("data",mes);
        mData.put("initiator",id);
        if(mps != null){
            for (Map.Entry<String, Chat> entry : mps.entrySet()) {
                entry.getValue().sendMessage(mData.toString());
            };
        };
        return true;
    }
}
